﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Security;
using ScrumTable.Common.Test;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Generic.Test.Mocks;
using ScrumTable.DL.Data.Local.Config.Driver;
using ScrumTable.DL.Data.Local.Config.Schema;

#endregion

namespace ScrumTable.DL.Data.Local.Test
{
    /// <summary>
    /// This is the test class for Local.DataObject class.
    /// </summary>
    [TestClass]
    public class DataObjectTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DataContext _context;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataObjectTest instance.
        /// </summary>
        public DataObjectTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            _context = new DataContext(new DataContextInfoStub(
                TestContext.TestDir,
                dci => new DataSchemaSelector(dci),
                dci => new DriverConfigSelector(dci)));
            _context.Connection.Connect(
                new ConnectionInformationManager(ConnectionInformation.Empty),
                result => { },
                Resources.GetProjectTestDataFilePath(TestContext.TestDir) );

            _context.ImportData(Resources.FullProject1IdChain, new NullDataImportMonitor());
        }

        /// <summary>
        /// Use TestCleanup to run code after each test has run.
        /// </summary>
        [TestCleanup]
        public void TestCleanup()
        {
            _context.Dispose();
            _context = null;
        }

        [TestMethod]
        public void TestUpdateTask()
        {
            Assert.IsTrue(UpdateTask(true, "[New Name]"));
        }

        [TestMethod]
        public void TestUpdateTaskWithoutEvents()
        {
            Assert.IsFalse(UpdateTask(false, "[New Name]"));
        }

        [TestMethod]
        public void TestUpdateTaskWithSameValue()
        {
            Assert.IsFalse(UpdateTask(true, (string)_context.Storage[_context.Schema.TaskSchema].GetById(Resources.Task1IdChain, Resources.Task1Id)[_context.Schema.TaskSchema.Name]));
        }


        private bool UpdateTask(bool doFireEvent, string newName)
        {
            if (doFireEvent)
            {
                using (_context.DataCache.EnableEventPropagation())
                {
                    return UpdateTask(newName);
                }
            }
            return UpdateTask(newName);
        }

        private bool UpdateTask(string newName)
        {
            IDataObject dataObject = _context.Storage[_context.Schema.TaskSchema].GetById(Resources.Task1IdChain, Resources.Task1Id);
            Assert.IsNotNull(dataObject);
            return AssertUtil.FiresChangeEvents(dataObject, o => dataObject[_context.Schema.TaskSchema.Name] = newName, "Name");
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
